package com.aiti.lulian.controller.gongxin;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.MyCollectionDto;
import com.aiti.lulian.entity.MyCollection;
import com.aiti.lulian.entity.ShowEnterpriseServiceEntity;
import com.aiti.lulian.gxmh.vo.GxmhPolicyVo;
import com.aiti.lulian.service.IMyCollectionService;
import com.aiti.lulian.vo.ShowActivityInfoVo;
import com.aiti.lulian.vo.gongxin.FundVo;
import com.aiti.lulian.vo.gongxin.SupplyAndDemandVo;
import com.aiti.lulian.vo.xlb.XlbEnterpriseVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "资金控制层")
@RestController
@RequestMapping("/collection")
public class MyCollectionController extends WebController {

    @Autowired
    private IMyCollectionService myCollectionService;

    @ApiOperation("判断当前数据是否已被收藏")
    @GetMapping("/isCollected")
    public Message collect(@RequestParam("module") String module,
                           @RequestParam("id") String id,
                           @RequestParam("userId") String userId) {
        MyCollection myCollection = myCollectionService.getOne(new LambdaQueryWrapper<MyCollection>().eq(MyCollection::getModule, module).eq(MyCollection::getId, id).eq(MyCollection::getCreator, userId));
        if (myCollection != null) {
            return Message.success(true);
        } else {
            return Message.success(false);
        }
    }

    @ApiOperation("收藏")
    @PostMapping
    public Message collect(@RequestBody MyCollectionDto dto) {
        dto.setCreator(dto.getUserId());
        myCollectionService.collect(dto);
        return Message.success();
    }

    @ApiOperation("取消收藏")
    @DeleteMapping("/cancel")
    public Message cancel(MyCollectionDto dto) {
        myCollectionService.remove(new LambdaQueryWrapper<MyCollection>().eq(MyCollection::getModule, dto.getModule()).eq(MyCollection::getId, dto.getId()).eq(MyCollection::getCreator, dto.getUserId()));
        return Message.success();
    }

    @ApiOperation("查询我的收藏列表")
    @GetMapping("/list")
    public Message getList(MyCollectionDto dto) {
        String module = dto.getModule();
        if (StrUtil.equals("1", module)) {
            // 服务模块
            Page<ShowEnterpriseServiceEntity> page = startPage();
            List<ShowEnterpriseServiceEntity> list = myCollectionService.getServiceCollectionList(page, dto);
            return Message.success(getPageResult(list, dto.getPageNum(), dto.getPageSize(), page.getTotal()));
        } else if (StrUtil.equals("2", module)) {
            // 活动模块
            Page<ShowActivityInfoVo> page = startPage();
            List<ShowActivityInfoVo> list = myCollectionService.getActivityCollectionList(page, dto);
            return Message.success(getPageResult(list, dto.getPageNum(), dto.getPageSize(), page.getTotal()));
        } else if (StrUtil.equals("3", module)) {
            // 资金模块
            Page<FundVo> page = startPage();
            List<FundVo> list = myCollectionService.getFundCollectionList(page, dto);
            return Message.success(getPageResult(list, dto.getPageNum(), dto.getPageSize(), page.getTotal()));
        } else if (StrUtil.equals("4", module)) {
            // 供需模块
            Page<SupplyAndDemandVo> page = startPage();
            List<SupplyAndDemandVo> list = myCollectionService.getSupplyAndDemandCollectionList(page, dto);
            return Message.success(getPageResult(list, dto.getPageNum(), dto.getPageSize(), page.getTotal()));
        } else if (StrUtil.equals("5", module)){
            // 政策模块
            IPage<GxmhPolicyVo> page = startPage();
            List<GxmhPolicyVo> list = myCollectionService.getGxmhPolicyCollectionList(page, dto);
            return Message.success(getPageResult(list, dto.getPageNum(), dto.getPageSize(), page.getTotal()));
        } else if (StrUtil.equals("6", module)){
            // 小蓝本企业模块
            IPage<XlbEnterpriseVo> page = startPage();
            List<XlbEnterpriseVo> list = myCollectionService.getXlbEnterpriseCollectionList(page, dto);
            return Message.success(getPageResult(list, dto.getPageNum(), dto.getPageSize(), page.getTotal()));
        }
        return Message.fail("查询失败");
    }
}
